package com.eos.orm.c3p0.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.eos.common.tool.CoreKeyContext;
import com.eos.orm.c3p0.dao.AbstractC3p0Dao;
import com.eos.orm.c3p0.dao.DBConnectionManager;
import com.eos.orm.mybatis.model.BrokerageTempDetail;
import com.hf.utils.StringUtil;

public class BrokerageTempDetailDaoImpl  extends AbstractC3p0Dao<BrokerageTempDetail>{

	public String condition1(Map<String, String> map) {
		StringBuilder condition = new StringBuilder();
		if (null == map)
			return condition.append("1=0").toString();
		if (!StringUtil.isEmpty(map.get("id")))
			condition.append("id='").append(map.get("id")).append("' and ");
		if (!StringUtil.isEmpty(map.get("mobile")))
			condition.append("mobile like '").append(
					map.get("mobile")).append("%' and ");
		if (!StringUtil.isEmpty(map.get("bar_code")))
			condition.append("bar_code like '").append(
					map.get("bar_code")).append("%' and ");
		 
		if (!StringUtil.isEmpty(map.get("customer_id")))
			condition.append("customer_id=").append(map.get("customer_id"))
					.append(" and ");
		if (!StringUtil.isEmpty(map.get("start_time")))
			condition.append("settlement_account>='").append(map.get("start_time"))
					.append("' and ");
		
		if (!StringUtil.isEmpty(map.get("end_time")))
			condition.append("settlement_account<='").append(map.get("end_time"))
					.append("' and ");
		condition.append("1=1");
		if(!StringUtil.isEmpty(map.get("order_by"))){
			condition.append(map.get("order_by"));
		}
		return condition.toString();
	}
	
	public List<BrokerageTempDetail> findPage(Map<String, String> map){
		List<BrokerageTempDetail> templist = new ArrayList<BrokerageTempDetail>();
		if (null == map)
			return templist;
		Connection connection = DBConnectionManager.getConnection();
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		String sql = StringUtil.replace(StringUtil.replace(StringUtil.replace(
				CoreKeyContext.getCoreKey("tabs_page_sqlscript"),
				"$table_name$", map.get("table_name")), "$condition1$",
				condition1(map)), "$condition2$", condition2(map));
		try {
			pstmt = connection.prepareStatement(sql);
			rs = pstmt.executeQuery();
			while (rs.next()) {
				BrokerageTempDetail temp = new BrokerageTempDetail();
				temp.setId(rs.getInt("id"));
				temp.setBarCode(rs.getString("bar_code"));
				temp.setBrokerageMoney(rs.getString("brokerage_money"));
				temp.setCityName(rs.getString("city_name"));
				temp.setComboId(rs.getInt("combo_id"));
				temp.setCustomerId(rs.getInt("customer_id"));
				temp.setInsertTime(rs.getString("insert_time"));
				temp.setMobile(rs.getString("mobile"));
				temp.setNetIdTime(rs.getString("net_id_time"));
				temp.setOrderId(rs.getString("order_id"));
				temp.setSalesId(rs.getInt("sales_id"));
				temp.setSettlementAccount(rs.getString("settlement_account"));
				temp.setStatus(rs.getString("status"));
				
				templist.add(temp);
				temp = null;
			}
		} catch (SQLException e) {
			System.out
					.println("product_model exec findPage operator is fail,Exception is :"
							+ e.getMessage());
		} finally {
			freeResource(connection, pstmt, rs);
		}
		return templist;
	}
	
	public boolean save(List<BrokerageTempDetail> btdList){
		Connection connection = DBConnectionManager.getConnection();
		String sql = "insert into `brokerage_temp_detail` (`customer_id`, `settlement_account`, `city_name`, `mobile`, `bar_code`, `net_id_time`, `brokerage_money`, `order_id`, `combo_id`, `sales_id`, `insert_time`, `status`) values(?,?,?,?,?,?,?,?,?,?,?,?) ";
		boolean flag = false;
		PreparedStatement pstm = null;
		try {
			pstm = connection.prepareStatement(sql);
			for(BrokerageTempDetail bdt : btdList){
				pstm.setInt(1, bdt.getCustomerId());
				pstm.setString(2, bdt.getSettlementAccount());
				pstm.setString(3, bdt.getCityName());
				pstm.setString(4, bdt.getMobile());
				pstm.setString(5, bdt.getBarCode());
				pstm.setString(6, bdt.getNetIdTime());
				pstm.setString(7, bdt.getBrokerageMoney());
				pstm.setString(8, bdt.getOrderId());
				pstm.setInt(9, bdt.getComboId());
				pstm.setInt(10, bdt.getSalesId());
				pstm.setString(11, bdt.getInsertTime());
				pstm.setString(12, bdt.getStatus());
				pstm.addBatch();
			}
			int[] rlts = pstm.executeBatch();
			if(null != rlts && rlts.length > 0){
				flag = true;
			}
		} catch (SQLException e) {
			e.printStackTrace();
			flag = false;
		}finally {
			freeResource(connection, pstm, null);
		}
		return flag;
	}
}
